<?php

namespace App\Domain\Dict\Dao;

use App\Domain\Dict\Models\DictModel;
use App\Domain\Dict\Models\DictTypeModel;

class DictDao
{
    public function findByTypeCode($type_code)
    {
        return DictModel::where('type_code', $type_code)->get(['id', 'type_code', 'code', 'name']);
    }

    public function findByType($id)
    {
        return DictTypeModel::where('id', $id)->first();
    }

    public function findByDictId($id)
    {
        return DictModel::where('id', $id)->first();
    }

    public function findByDictName($name)
    {
        return DictModel::where('id', $name)->first();
    }

    public function findByTypeName($name)
    {
        return DictTypeModel::where('name', $name)->first();
    }

    public function findByMaterialCode($material_dict_code)
    {
        return DictModel::where('id', $material_dict_code)->first();
    }

    public function findNew()
    {
        return DictTypeModel::orderBy('id', 'desc')->first();
    }


    /**
     * 创建类型
     * @param $params
     * @return DictModel
     */
    public function create($params)
    {
        $params = array_filter_null($params);
        return DictModel::create($params);
    }

    /**
     * 修改类型
     * @param DictModel $model
     * @param $param
     * @return DictModel
     */
    public function update(DictModel $model, $param)
    {
        $param = array_filter_null($param);
        $model->fill($param)->save();
        return $model;
    }

    /**
     * 创建类型
     * @param $params
     * @return DictTypeModel
     */
    public function createType($params)
    {
        $params = array_filter_null($params);
        return DictTypeModel::create($params);
    }

    /**
     * 修改类型
     * @param DictTypeModel $model
     * @param $param
     * @return DictTypeModel
     */
    public function updateType(DictTypeModel $model, $param)
    {
        $param = array_filter_null($param);
        $model->fill($param)->save();
        return $model;
    }
}
